package cn.itcast.bos.web.action.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.dao.base.StandardRepository;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
@Actions
public class StandardAction extends ActionSupport implements ModelDriven<Standard> {

	private Standard standard = new Standard();
	
	private Integer page;
	private Integer rows;
	private Integer deleteId;
	
	

	public Integer getDeleteId() {
		return deleteId;
	}

	public void setDeleteId(Integer deleteId) {
		this.deleteId = deleteId;
	}

	public Integer getPage() {
		return page;
	}

	public void setPage(Integer page) {
		this.page = page;
	}

	public Integer getRows() {
		return rows;
	}

	public void setRows(Integer rows) {
		this.rows = rows;
	}

	@Autowired
	private StandardService standardService;
	
	@Autowired
	private StandardRepository repository;

	@Override
	public Standard getModel() {
		return standard;
	}

	// 添加操作
	@Action(value = "standard_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/base/standard.html") })
	public String save() {
		System.out.println("standard:" + standard);
		standardService.save(standard);
		return SUCCESS;
	}

	// 分页查询操作
	@Action(value = "standard_page_query", results = {
			@Result(name = "success", type = "json")})
	public String findStandard() {
		//查询分页数据
		Page<Standard> pageData = repository.findAll(new PageRequest(page-1, rows));
		
		//准备好datagrid需要的数据格式
		Map<String, Object> data = new HashMap<>();
		data.put("total", pageData.getTotalElements());
		data.put("rows", pageData.getContent());
		ActionContext.getContext().getValueStack().push(data);
		return SUCCESS;
	}
	
	//删除操作
	@Action(value="deleteStandard")
	public void deleteStandard(){
		//System.out.println("deleteId:"+deleteId);
		repository.delete(deleteId);
		//findStandard();
		//System.out.println(standard);
	}
	
	// 查询所有收派标准方法
		@Action(value = "standard_findAll", results = { @Result(name = "success", type = "json") })
		public String findAll() {
			List<Standard> standards = standardService.findAll();
			ActionContext.getContext().getValueStack().push(standards);
			return SUCCESS;
		}

}
